# SparkQtQmlMacros.cmake
# 
# 此模块用于构建和打包 Qt 项目的 qml 模块。
# 本模块提供了一系列宏（函数）来简化 qml 模块的构建和自动化过程。
# 
# 使用方法：
# 1. 包含此模块
# 2. 使用 spark_add_qml_module 构建 qml 模块
# 3. 模块即目标，可以使用 set_target_properties 设置属性
# 4. 模块即目标，可以对其进行配置依赖或者定义输出位置
# 
# 注意： 确保在使用此模块之前已包含了 SparkFindQt*.cmake 模块


# spark_add_qml_module(module [sources])
# 构建一个 qml 模块
    # 根据 模块名 和 源文件列表 构建一个 qml 模块
    # 构建完成后，在目标所在目录创建一个 qmldir 文件
    # 在 qmldir 文件中写入基本信息
function(spark_add_qml_module module)
    spark_add_shared_library(${module}
        ${ARGN}
    )
    # TODO: 链接 qt 库由外部调用者决定
    # target_link_qt5(${plugin})
    # target_link_qt5_quick(${plugin})

    # 设置输出目录
    set_target_properties(${module}
        PROPERTIES
            OUTPUT_NAME $<IF:$<BOOL:$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>>,$<LOWER_CASE:$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>>,$<LOWER_CASE:${module}>>
            LIBRARY_OUTPUT_DIRECTORY 
                ${CMAKE_CURRENT_BINARY_DIR}/${module}
    )

    # 目标构建完成后执行
    # 在目标所在目录创建一个 qmldir 文件
    # 在 qmldir 文件中写入
    add_custom_command(TARGET ${module}
        POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E rm -f "$<TARGET_FILE_DIR:${module}>/qmldir"
            COMMAND ${CMAKE_COMMAND} -E touch "$<TARGET_FILE_DIR:${module}>/qmldir"
            COMMAND ${CMAKE_COMMAND} -E echo  "module    $<IF:$<BOOL:$<TARGET_PROPERTY:${module},QMLDIR_MODULE>>,$<TARGET_PROPERTY:${module},QMLDIR_MODULE>,${module}>"          >> $<TARGET_FILE_DIR:${module}>/qmldir
            COMMAND ${CMAKE_COMMAND} -E echo  "classname $<IF:$<BOOL:$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>>,$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>,${module}>"    >> $<TARGET_FILE_DIR:${module}>/qmldir
            COMMAND ${CMAKE_COMMAND} -E echo  "typeinfo  $<LOWER_CASE:${module}>.qmltypes"                                                                                       >> $<TARGET_FILE_DIR:${module}>/qmldir
            COMMAND ${CMAKE_COMMAND} -E echo  "plugin    $<IF:$<BOOL:$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>>,$<LOWER_CASE:$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>>,$<LOWER_CASE:${module}>>"          >> $<TARGET_FILE_DIR:${module}>/qmldir
    )
    if(EXISTS "${Qt${QT_VERSION_MAJOR}_DIR}/../../../bin/qmlplugindump")
        add_custom_command(TARGET ${module}
            POST_BUILD
                COMMAND "${Qt${QT_VERSION_MAJOR}_DIR}/../../../bin/qmlplugindump"
                        -nonrelocatable -noinstantiate
                          "${module}" "1.0" "$<TARGET_FILE_DIR:${module}>/.." -output $<TARGET_FILE_DIR:${module}>/$<LOWER_CASE:${module}>.qmltypes
            USES_TERMINAL
        )
    else()
        add_custom_command(TARGET ${module}
            POST_BUILD
                COMMAND QT_SELECT=qt${QT_VERSION_MAJOR} qmlplugindump
                        -nonrelocatable -noinstantiate
                         "${module}" "1.0" "$<TARGET_FILE_DIR:${module}>/.." -output $<TARGET_FILE_DIR:${module}>/$<LOWER_CASE:${module}>.qmltypes
            USES_TERMINAL
        )
    endif(EXISTS "${Qt${QT_VERSION_MAJOR}_DIR}/../../../bin/qmlplugindump")
endfunction(spark_add_qml_module module)


function(spark_add_qml_module_path module)
    spark_add_shared_library_path(${module}
        ${ARGN}
    )
    # TODO: 链接 qt 库由外部调用者决定
    # target_link_qt5(${plugin})
    # target_link_qt5_quick(${plugin})

    # 设置输出目录
    set_target_properties(${module}
        PROPERTIES
            OUTPUT_NAME $<IF:$<BOOL:$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>>,$<LOWER_CASE:$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>>,$<LOWER_CASE:${module}>>
            LIBRARY_OUTPUT_DIRECTORY 
                ${CMAKE_CURRENT_BINARY_DIR}/${module}
    )

    # 目标构建完成后执行
    # 在目标所在目录创建一个 qmldir 文件
    # 在 qmldir 文件中写入
    add_custom_command(TARGET ${module}
        POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E rm -f "$<TARGET_FILE_DIR:${module}>/qmldir"
            COMMAND ${CMAKE_COMMAND} -E touch "$<TARGET_FILE_DIR:${module}>/qmldir"
            COMMAND ${CMAKE_COMMAND} -E echo  "module    $<IF:$<BOOL:$<TARGET_PROPERTY:${module},QMLDIR_MODULE>>,$<TARGET_PROPERTY:${module},QMLDIR_MODULE>,${module}>"          >> $<TARGET_FILE_DIR:${module}>/qmldir
            COMMAND ${CMAKE_COMMAND} -E echo  "classname $<IF:$<BOOL:$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>>,$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>,${module}>"    >> $<TARGET_FILE_DIR:${module}>/qmldir
            COMMAND ${CMAKE_COMMAND} -E echo  "typeinfo  $<LOWER_CASE:${module}>.qmltypes"                                                                                       >> $<TARGET_FILE_DIR:${module}>/qmldir
            COMMAND ${CMAKE_COMMAND} -E echo  "plugin    $<IF:$<BOOL:$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>>,$<LOWER_CASE:$<TARGET_PROPERTY:${module},QMLDIR_CLASSNAME>>,$<LOWER_CASE:${module}>>"          >> $<TARGET_FILE_DIR:${module}>/qmldir
    )
    if(EXISTS "${Qt${QT_VERSION_MAJOR}_DIR}/../../../bin/qmlplugindump")
        add_custom_command(TARGET ${module}
            POST_BUILD
                COMMAND "${Qt${QT_VERSION_MAJOR}_DIR}/../../../bin/qmlplugindump"
                        -nonrelocatable -noinstantiate
                          "${module}" "1.0" "$<TARGET_FILE_DIR:${module}>/.." -output $<TARGET_FILE_DIR:${module}>/$<LOWER_CASE:${module}>.qmltypes
            USES_TERMINAL
        )
    else()
        add_custom_command(TARGET ${module}
            POST_BUILD
                COMMAND QT_SELECT=qt${QT_VERSION_MAJOR} qmlplugindump 
                        -nonrelocatable -noinstantiate
                         "${module}" "1.0" "$<TARGET_FILE_DIR:${module}>/.." -output $<TARGET_FILE_DIR:${module}>/$<LOWER_CASE:${module}>.qmltypes
            USES_TERMINAL
        )
    endif(EXISTS "${Qt${QT_VERSION_MAJOR}_DIR}/../../../bin/qmlplugindump")
endfunction(spark_add_qml_module_path module)

